import BaseActionCableConnector from './BaseActionCableConnector'
import { ON_AGENT_MESSAGE_RECEIVED } from './constants/widgetBusEvents'

class ActionCableConnector extends BaseActionCableConnector {
  constructor (app, pubsubToken, accountId, contactId, authToken) {
    super(app, pubsubToken, accountId, contactId, authToken)
    this.CancelTyping = []
    this.events = {
      'message.created': this.onMessageCreated
    }
  }

  onMessageCreated = data => {
    console.log(data)
    this.app.$store
      .dispatch('conversation/addOrUpdateMessage', data)
      .then(() => window.bus.$emit(ON_AGENT_MESSAGE_RECEIVED))
  }
}

// export default ActionCableConnector

export const createActionCable = (app, pubsubToken, accountId, contactId, authToken) => {
  return new ActionCableConnector(app, pubsubToken, accountId, contactId, authToken)
}
